package com.wan.data.mysql.util;

public enum DbColumnType {

    DECIMAL("DECIMAL",  65L, 2,Format.F_2),
    TINYINT("TINYINT", 3L,3L, Format.F_1),
    BOOLEAN("TINYINT",  1L,2L, Format.F_1),
    SMALLINT("SMALLINT",  6L, Format.F_1),
    INT("INT",  11L, Format.F_1),
    FLOAT("FLOAT",  10L,12L, 2,Format.F_2),
    DOUBLE("DOUBLE", 12L,22L, 2,Format.F_2),
    NULL("NULL", 0L, Format.F_0),
    TIMESTAMP("TIMESTAMP", 26L, Format.F_0),
    BIGINT("BIGINT",  20L, Format.F_1),
    DATE("DATE", 10L, Format.F_0),
    TIME("TIME", 16L, Format.F_0),
    DATETIME("DATETIME",  26L, Format.F_0),
    YEAR("YEAR",  4L, Format.F_0),
    VARCHAR("VARCHAR",  255L,65535L, Format.F_1),
    VARBINARY("VARBINARY",  255L,65535L, Format.F_1),
    BIT("BIT", 1L, Format.F_1),
    JSON("JSON",  1073741824L, Format.F_0),
    TINYBLOB("TINYBLOB", 255L, Format.F_0),
    TINYTEXT("TINYTEXT", 255L, Format.F_0),
    MEDIUMBLOB("MEDIUMBLOB",  16777215L, Format.F_0),
    MEDIUMTEXT("MEDIUMTEXT", 16777215L, Format.F_0),
    LONGBLOB("LONGBLOB", 4294967295L, Format.F_0),
    LONGTEXT("LONGTEXT", 4294967295L, Format.F_0),
    BLOB("BLOB", 65535L, Format.F_0),
    TEXT("TEXT",  65535L, Format.F_0),
    CHAR("CHAR",  100L,255L, Format.F_1),
    BINARY("BINARY", 255L, Format.F_1),
    GEOMETRY("GEOMETRY", 65535L, Format.F_0),
    UNKNOWN("UNKNOWN", 65535L, Format.F_0)
    ;
    /** 类型名称 */
    private final String name;
    /** 最大长度 */
    private final long maxLen;
    /** 默认长度 */
    private final long defaultLen;
    /** 默认小数点位数 */
    private final int pointLen;
    /** 格式化 */
    private final Format format;

    DbColumnType(String name, long defaultLen,long maxLen, int pointLen, Format format) {
        this.name = name;
        this.maxLen = maxLen;
        this.defaultLen = defaultLen;
        this.pointLen = pointLen;
        this.format = format;
    }

    DbColumnType(String name,long defaultLen, long maxLen, Format format) {
        this(name,defaultLen,maxLen,0,format);
    }

    DbColumnType(String name, long maxLen, Format format) {
        this(name,maxLen,maxLen,0,format);
    }

    public long getMaxLen() {
        return maxLen;
    }

    public String format(long len, int pointLen){
        long length = len;
        if(len == -1) {
            length = defaultLen;
        }else
        if(len > maxLen){
            length = maxLen;
        }
        if(pointLen == 0 || pointLen > length){
            pointLen = this.pointLen;
        }
        return format.format(this,length,pointLen);
    }

    private enum Format{
        //无长度指定
        F_0{
            @Override
            public String format(DbColumnType columnType, long len, int pointLen) {
                return columnType.name;
            }
        },
        //指定长度
        F_1{
            @Override
            public String format(DbColumnType columnType, long len, int pointLen) {
                return String.format("%s(%s)",columnType.name,len);
            }
        },
        //指定长度和精度
        F_2{
            @Override
            public String format(DbColumnType columnType, long len, int pointLen) {
                return String.format("%s(%s,%s)",columnType.name,len,pointLen);
            }
        }
        ;
        public abstract String format(DbColumnType columnType,long len,int pointLen);
    }

}
